package com.example.myapplication.database;

import android.annotation.SuppressLint;
import android.content.Context;

import com.example.myapplication.bean.OrderBean;
import com.example.myapplication.dao.OrderBeanDao;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名：OrderBeanDBUtils
 * 描述：TOOD
 */
public class OrderBeanDBUtils {


    public static final int VERSION = 1;

    private static OrderBeanDBUtils sqliteDB;



    private OrderBeanDBUtils(Context context) {

    }

    /**
     * ��ȡSqliteDBʵ��
     * @param context
     */
    public synchronized static OrderBeanDBUtils getInstance(Context context) {
        if (sqliteDB == null) {
            sqliteDB = new OrderBeanDBUtils(context);
        }
        return sqliteDB;
    }
    /**
     * 修改信息
     * @param context
     * @param shopAddress
     *  ,,,,,,
     */
    public void change(Context context, OrderBean shopAddress) {
        OrderBeanDao orderBeanDao = GreenDaoManager.getmInstance().getDaoSession().getOrderBeanDao();
        orderBeanDao.update(shopAddress);
    }


    public void delete(Context context,long id) {
        OrderBeanDao orderBeanDao = GreenDaoManager.getmInstance().getDaoSession().getOrderBeanDao();
        orderBeanDao.deleteByKey(id);
    }

    /**
     *
     * @param orderBean
     * @return
     */
    public void   insert(OrderBean orderBean) {

        OrderBeanDao orderBeanDao = GreenDaoManager.getmInstance().getDaoSession().getOrderBeanDao();
        orderBeanDao.insert(orderBean);
    }
    @SuppressLint("Range")
    public List<OrderBean> getListByUserid(String user_id,String state){
        OrderBeanDao orderBeanDao = GreenDaoManager.getmInstance().getDaoSession().getOrderBeanDao();
        List<OrderBean> shopAddressList = new ArrayList<>();
        List<OrderBean> list = new ArrayList<>();
        list = orderBeanDao.loadAll();
        if (list !=null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                if (user_id.equals(list.get(i).getUser_id()) && state.equals(list.get(i).getState())){
                    shopAddressList.add(list.get(i));
                }
            }
        }

        return shopAddressList;
    }


    @SuppressLint("Range")
    public OrderBean getOrderById(long id){
        OrderBean shopAddress = null;
        OrderBeanDao orderBeanDao = GreenDaoManager.getmInstance().getDaoSession().getOrderBeanDao();

        List<OrderBean> list = new ArrayList<>();
        list = orderBeanDao.loadAll();
        if (list !=null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                if (id== list.get(i).getId()){
                   return list.get(i);
                }
            }
        }
        return shopAddress;
    }

    @SuppressLint("Range")
    public List<OrderBean> getOrderByMerchatid(long mecherid,String state){

        OrderBeanDao orderBeanDao = GreenDaoManager.getmInstance().getDaoSession().getOrderBeanDao();
        List<OrderBean> shopAddressList = new ArrayList<>();
        List<OrderBean> list = new ArrayList<>();
        list = orderBeanDao.loadAll();
        if (list !=null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                if (mecherid == list.get(i).getMerchatid() && state.equals(list.get(i).getState())){
                    shopAddressList.add(list.get(i));
                }
            }
        }
        return shopAddressList;
    }
}
